package com.xyz.controller;

import com.xyz.aop.Param;
import com.xyz.service.EmployeeService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("system")
public class EmployeeController {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("selectEmployee")
    public DataResult selectEmployee(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String employeeName,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String roleId
    ) {
        try {
            return employeeService.selectEmployee(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), employeeName, roleId);
        } catch (Exception e) {
            logger.error("selectEmployee", e);
            return DataResult.build9500();
        }
    }

    @ApiOperation(value = "查询出空闲员工状态列表")
    @PostMapping("ruleFreeTimeList")
    public DataResult ruleFreeTimeList(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = true) String employeeTime
    ) {
        try {
            return employeeService.ruleFreeTimeList(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), employeeTime);
        } catch (Exception e) {
            logger.error("ruleFreeTimeList", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEmployee")
    public DataResult insertEmployee(
            LoginInfo li,
            @Param String employeeName,
            @Param String phoneNumber,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String position,
            @Param(isRequired = false) String headFileCode,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String isMessage,
            @Param String teamGroup
    ) {
        try {
            return employeeService.insertEmployee(li, employeeName, phoneNumber, Integer.parseInt(position),
                    headFileCode, Integer.parseInt(isMessage),teamGroup);
        } catch (Exception e) {
            logger.error("insertEmployee", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEmployee")
    public DataResult updateEmployee(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId,
            @Param String employeeName,
            @Param String phoneNumber,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String position,
            @Param(isRequired = false) String headFileCode,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String isMessage,
            @Param String teamGroup
    ) {
        try {
            return employeeService.updateEmployee(li, Long.parseLong(employeeId), employeeName, phoneNumber,
                    Integer.parseInt(position), headFileCode, Integer.parseInt(isMessage),teamGroup);
        } catch (Exception e) {
            logger.error("updateEmployee", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteEmployee")
    public DataResult deleteEmployee(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId
    ) {
        try {
            return employeeService.deleteEmployee(li, Long.parseLong(employeeId));
        } catch (Exception e) {
            logger.error("deleteEmployee", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEmployeeUser")
    public DataResult updateEmployeeUser(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String userId
    ) {
        try {
            return employeeService.updateEmployeeUser(li, Long.parseLong(employeeId), Long.parseLong(userId));
        } catch (Exception e) {
            logger.error("updateEmployeeUser", e);
            return DataResult.build9500();
        }
    }

}
